import { defineNuxtPlugin } from "#app";

import { createDiscreteApi } from "naive-ui";

export default defineNuxtPlugin((nuxtApp) => {
  const bar = ref(null);
  nuxtApp.hook("app:mounted", () => {
    const { loadingBar } = createDiscreteApi(["loadingBar"]);
    if (!bar.value) {
      bar.value = loadingBar;
    }
    //console.log("app:mounted");
  });
  nuxtApp.hook("page:start", () => {
    bar.value?.start();
    //console.log("page:start");
  });
  nuxtApp.hook("page:finish", () => {
    setTimeout(() => {
      bar.value?.finish();
    }, 150);
    //console.log("page:finish");
  });
  nuxtApp.hook("app:error", () => {
    if (process.client) {
      bar.value?.finish();
      //console.log("app:error");
    }
  });
});
